﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using NHibernate.Criterion;
using Project.Model;
using Project.Common;
using System.Linq;

public partial class Common_SelectPlantable : BasePage
{
    //创建查询器
    IList<ICriterion> queryList = new List<ICriterion>();
    // 排序字段
    Order order;

    /// <summary>
    /// 计划类别
    /// </summary>
    protected string JhType { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        JhType = base.QueryString("jhtype");
        
        if (!IsPostBack)
        {
            IQueryable<DPlantable> list = ManagerFactory.DPlantableManager.GetAll();
            JhTypeDropDownList.DataSource = list;
            foreach (DPlantable item in list)
            {
                if (JhTypeDropDownList.Items.IndexOf(new ListItem(item.JhType, item.JhType)) == -1)
                    JhTypeDropDownList.Items.Add(new ListItem(item.JhType, item.JhType));
            }
            //JhTypeDropDownList.DataTextField = "JhType";
            //JhTypeDropDownList.DataTextField = "JhType";
            //JhTypeDropDownList.DataBind();
            JhTypeDropDownList.Items.Insert(0, new ListItem("全部", ""));

            this.QuerySelect();
        }
    }

    #region 搜索

    protected void searchButton_Click(object sender, EventArgs e)
    {
        this.QuerySelect();
    }

    private void QuerySelect()
    {
        if (JhTypeDropDownList.Items.FindByValue(JhType) != null)
        {
            JhTypeDropDownList.ClearSelection();
            JhTypeDropDownList.Items.FindByValue(JhType).Selected = true;
        }
        
        // 传入条件
        // 添加自定义条件
        if (!string.IsNullOrEmpty(JhType))
            queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.JhType), JhType));
        if (!string.IsNullOrEmpty(txt_Name.Text))
            queryList.Add(Restrictions.Like(PropertyNameHelper.Resolve<Plantable>(d => d.Jhname), txt_Name.Text.Trim(), MatchMode.Anywhere));

        BindGrid();
    }


    #endregion

    private void BindGrid()
    {
        AspNetPager1.RecordCount = ManagerFactory.PlantableManager.GetCount(queryList);
        IList<Plantable> list = ManagerFactory.PlantableManager.GetPageList(AspNetPager1, queryList, order);
        GridView1.DataSource = list;
        GridView1.DataBind();
    }

    #region gridview绑定及命令事件

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            //string cssName;
            //if (e.Row.RowState == DataControlRowState.Alternate) cssName = "grid_alt_item";
            //else cssName = "grid_item";
            //e.Row.Attributes.Add("onmouseout", "this.className='" + cssName + "'");
            //e.Row.Attributes.Add("onmouseover", "this.className='" + "gridSelectedItem" + "'");
            RadioButton radbtn = new RadioButton();

            int index = e.Row.RowIndex;
            if (index >= 0)
            {
                radbtn = (RadioButton)e.Row.FindControl("ra");

                string UCode = GridView1.DataKeys[index].Value.ToString().Trim();

                if (UCode == "")
                {
                    radbtn.Visible = false;
                }

                if (radbtn != null)
                {
                    radbtn.Attributes["onclick"] = "SelectRadioButton(this)";
                }

            }
        }

    }


    #endregion

    #region 分页

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        this.QuerySelect();
    }

    #endregion

    #region 排序

    public string SortD
    {
        get
        {
            if (ViewState["SortD"] == null)
            {
                switch (GridView1.SortDirection)
                {
                    case SortDirection.Ascending:
                        ViewState["SortD"] = "ASC";
                        break;
                    case SortDirection.Descending:
                        ViewState["SortD"] = "DESC";
                        break;
                }
            }
            return Convert.ToString(ViewState["SortD"]);
        }
        set
        {
            ViewState["SortD"] = value;
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        SortD = (SortD.ToLower() == "asc" ? "DESC" : "ASC");
        order = new Order(e.SortExpression, SortD.ToLower() == "ASC".ToLower());
    }
    protected void GridView1_Sorted(object sender, EventArgs e)
    {
        this.QuerySelect();
    }

    #endregion

    protected void btnSave_Click(object sender, EventArgs e)
    {
        RadioButton ra = new RadioButton();
        string strpkid = "";
        string strItemName = "";

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            ra = (RadioButton)GridView1.Rows[i].FindControl("ra");
            if (ra != null && ra.Checked == true)
            {
                strpkid += ra.Attributes["value"].ToString();
                strItemName += GridView1.Rows[i].Cells[1].Text;
            }
        }

        if (strpkid != "")
        {
            if (strpkid.IndexOf("$") > -1)
            {
                strpkid = strpkid.Remove(strpkid.Length - 1).Replace("&nbsp;", "");
            }
        }
        else
        {
            AlertAjax("请您选择项目！", btnSave);
            return;
        }

        if (strItemName != "" && strItemName.IndexOf("$") > -1)
            strItemName = strItemName.Remove(strItemName.Length - 1).Replace("&nbsp;", "");

        StringBuilder str = new StringBuilder();
        str.Append("var Item=new Object; \r\n");
        str.Append("Item.pkid=\"" + strpkid + "\";  \r\n");
        //str.Append("Item.ItemName=\"" + strItemName.Replace("\"", "\\\"") + "\";  \r\n");
        #region 添加多个属性
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            ra = (RadioButton)GridView1.Rows[i].FindControl("ra");
            if (ra != null && ra.Checked == true)
            {
                foreach (Control RowsControl in GridView1.Rows[i].Controls)
                {
                    foreach (Control item in RowsControl.Controls)
                    {
                        if (item.ToString() == typeof(TextBox).ToString())
                        {
                            TextBox tb = item as TextBox;
                            str.Append("Item." + tb.Attributes["newfield"].ToString() + "=\"" + tb.Attributes["value"].ToString().Replace("\"", "\\\"") + "\";  \r\n");
                        }
                    }
                }
            }
        }
        //str.Append(base.AddSelectScriptStr(GridView1));
        #endregion
        str.Append("returnVal=Item;window.parent.cancel(); \r\n");
        ExecJsAjax(str.ToString(), btnSave);

    }

    protected void JhTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Response.Redirect(Request.Url.LocalPath + "?JhType=" + JhTypeDropDownList.SelectedValue);
        Response.Redirect(WebHelper.BuildUrl(Request.Url.ToString(), new string[] { "JhType" }, new string[] { JhTypeDropDownList.SelectedValue }));
    }
}
